package com.sf.dlb.service;

import com.sf.dlb.bean.NewRquestBean;
import com.sf.dlb.bean.VideoRquestBean;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.mongo.model.NewList;
import com.sf.dlb.mongo.model.VideoList;
import com.sf.dlb.mongo.model.VisitorRecord;
import com.sf.dlb.mongo.repository.NewListRepository;
import com.sf.dlb.mongo.repository.VideoListRepository;
import com.sf.dlb.mongo.repository.VisitorRecordRepository;
import com.sf.dlb.utils.DateUtils;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Created by jing.tan on 2018/5/21.
 */
@Service
public class VisitorRecordService {
    private static Logger logger = LoggerFactory.getLogger(VisitorRecordService.class);
    @Autowired
    private VisitorRecordRepository visitorRecordRepository;
    @Autowired
    private NewListRepository newListRepository;
    @Autowired
    private VideoListRepository videoListRepository;
    /**
     * 开始阅读
     * @param newRquestBean
     * @return
     */
    @Transactional
    public ResultVO startReadNew(NewRquestBean newRquestBean){
        logger.info("startReadNew start newRquestBean={} ", GsonUtils.getInstance().toJson(newRquestBean));
        //是否已读
        String newId=newRquestBean.getNewId();
        Long userId=Long.valueOf(newRquestBean.getUserId());
        VisitorRecord visitorRecord= visitorRecordRepository.findByVisitorIdAndReadTypeAndReadIdAndDelFlag(userId,"1",newId,"0");
        if(null!=visitorRecord){
            logger.info("startReadNew 此新闻已读 ");
            return ResultVOUtil.success(); //前端要求，不要提示；
        }
        //保存阅读记录
        visitorRecord=new VisitorRecord();
        visitorRecord.setVisitorId(userId);
        visitorRecord.setReadType("1");
        visitorRecord.setReadId(newId);
        NewList newListInfo= newListRepository.findByIdAndDelFlag(newId,"0");
        if(newListInfo!=null){
            visitorRecord.setNewsClassId(newListInfo.getClassId());
            visitorRecord.setNewsClassName(newListInfo.getClassName());
        }
        visitorRecord.setStartTime(new Date());
        visitorRecord.setCreateDate(new Date());
        visitorRecord.setUpdateDate(new Date());
        visitorRecordRepository.save(visitorRecord);
        return ResultVOUtil.success();
    }
    /**
     * 结束阅读
     * @param newRquestBean
     * @return
     */
    @Transactional
    public ResultVO endReadNew(NewRquestBean newRquestBean){
        logger.info("endReadNew start newRquestBean={} ",GsonUtils.getInstance().toJson(newRquestBean));
        String newId=newRquestBean.getNewId();
        Long userId=Long.valueOf(newRquestBean.getUserId());
        //是否已读
        VisitorRecord visitorRecord= visitorRecordRepository.findByVisitorIdAndReadTypeAndReadIdAndDelFlag(userId,"1",newId,"0");
        if(null==visitorRecord){
            logger.info("endReadNew 此新闻未读 ");
            return ResultVOUtil.error(ResponseConstant.ERROR,"");//前端要求；
        }
        //如果结束时间已存在，就不再计算
        Date endTimeD=visitorRecord.getEndTime();
        if(null!=endTimeD){
            logger.info("endReadNew 此新闻已结束阅读 ");
            return ResultVOUtil.error(ResponseConstant.ERROR,"");//前端要求；由于前端是统一处理错误信息；所以建议不要有提示；不然会导致领取dlb的提示弹不出来；重叠了；
        }
        //计算阅读时间
        Date startTime=visitorRecord.getStartTime();
        Date endTime=new Date();
        long compare= DateUtils.differTimeLong(startTime,endTime)/1000; //秒
        //记录结束阅读时间，状态
        visitorRecord.setEndTime(endTime);
        visitorRecord.setReadTimeLong((int)compare);
        visitorRecord.setUpdateDate(new Date());
        visitorRecordRepository.save(visitorRecord);
        return ResultVOUtil.success();
    }

    @Transactional
    public ResultVO startPlay(VideoRquestBean videoRquestBean){
        logger.info("startPlay start videoRquestBean={} ", GsonUtils.getInstance().toJson(videoRquestBean));
        //是否已读
        String videoId=videoRquestBean.getVideoId();
        Long userId=Long.valueOf(videoRquestBean.getUserId());
        VideoList videoList=videoListRepository.findByIdAndDelFlag(videoId,"0");
        if(videoList==null){
            logger.info("startPlay 此视频不存在 ");
            return ResultVOUtil.success();
        }
        VisitorRecord visitorRecord= visitorRecordRepository.findByVisitorIdAndReadTypeAndReadIdAndDelFlag(userId,"2",videoId,"0");
        if(null!=visitorRecord){
            logger.info("startPlay 此视频已播放 ");
            return ResultVOUtil.success();
        }
        //保存阅读记录
        visitorRecord=new VisitorRecord();
        visitorRecord.setVisitorId(userId);
        visitorRecord.setReadType("2");
        visitorRecord.setReadId(videoId);
        visitorRecord.setStartTime(new Date());
        visitorRecord.setCreateDate(new Date());
        visitorRecord.setUpdateDate(new Date());
        visitorRecordRepository.save(visitorRecord);
        return ResultVOUtil.success();
    }
    @Transactional
    public ResultVO endPlay(VideoRquestBean videoRquestBean){
        logger.info("endPlay start videoRquestBean={} ",GsonUtils.getInstance().toJson(videoRquestBean));
        String videoId=videoRquestBean.getVideoId();
        Long userId=Long.valueOf(videoRquestBean.getUserId());
        VisitorRecord visitorRecord= visitorRecordRepository.findByVisitorIdAndReadTypeAndReadIdAndDelFlag(userId,"2",videoId,"0");
        if(null==visitorRecord){
            logger.info("endPlay 此视频未开始播放 ");
            return ResultVOUtil.error(ResponseConstant.ERROR,"");
        }
        //如果结束时间已存在，就不再计算
        Date endTimeD=visitorRecord.getEndTime();
        if(null!=endTimeD){
            logger.info("endPlay 此视频已结束播放 ");
            return ResultVOUtil.error(ResponseConstant.ERROR,"");
        }
        //计算播放时间
        Date startTime=visitorRecord.getStartTime();
        Date endTime=new Date();
        long compare=DateUtils.differTimeLong(startTime,endTime)/1000; //秒
        //记录结束时间，状态
        visitorRecord.setEndTime(endTime);
        visitorRecord.setReadTimeLong((int)compare);
        visitorRecord.setUpdateDate(new Date());
        visitorRecordRepository.save(visitorRecord);
        return ResultVOUtil.success();
    }
}
